<?php
function webscience_preprocess_page(&$variables)
{
    $variables['footer_menu'] = menu_navigation_links('navigation');
}

function webscience_menu_link(array &$variables)
{
    $element  = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);

    $element['#attributes']['class'] = $element['#href'] == $_GET['q'] ? array('active') : array();

    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

function webscience_preprocess_node(&$variables)
{
    $node              = $variables['node'];
    $variables['date'] = format_date($node->created, 'custom', 'd.m.Y H:i');
}

function webscience_preprocess_username(&$variables)
{

    $user     = user_load($variables['uid']);
    $username = implode(' ', array(
        $user->field_profile_name['und'][0]['safe_value'],
        $user->field_profile_surname['und'][0]['safe_value'],
    ));

    if (strlen(trim($username))) {
        $variables['name'] = $username;
    }
}

function webscience_preprocess_comment(&$variables)
{
    $comment              = $variables['elements']['#comment'];
    $variables['created'] = format_date($comment->created, 'custom', 'd.m.Y H:i');
}

function phptemplate_preprocess_node(&$variables)
{
}

function webscience_form_comment_form_alter(&$form, &$form_state)
{

    $form['#attributes']['class'] = array('functions');

    $form['actions']['submit']['#value'] = 'Опубликовать';
    $form['actions']['submit']['#class'] = 'blueBtn';

    $form['author']['_author']['#title_display'] = 'none';
    $form['author']['name']['#title_display']    = 'none';

    $is_anonymous = $form['is_anonymous']['#value'];
    if ($is_anonymous) {
        $form['author']['name']['#type']  = 'hidden';
        $form['author']['name']['#theme'] = 'hidden';
        $form['author']['name']['#value'] = 'Anonymous';
    }
}

function webscience_form_search_form_alter(&$form, &$form_state)
{
    $form['advanced'] = array();
    //echo '<pre>';print_r($form);echo '</pre>';
}

function webscience_form_alter(&$form, &$form_state, $form_id)
{
    if ($form_id == 'search_block_form') {
        $form['search_block_form'] = array_merge($form['search_block_form'], array(
            '#title'          => '',
            '#title_display'  => 'invisible',
            '#default_value'  => 'Поиск по сайту',
            '#theme_wrappers' => array(),
            '#attributes'     => array('id' => 's', 'class' => ''),
        ));

        $form['actions']['#theme_wrappers'] = array();
        $form['actions']['submit']          = array(
            '#type'       => 'submit',
            '#id'         => 'searchSubmit',
            '#class'      => '',
            '#attributes' => array()
        );
    }
}

function webscience_theme($existing, $type, $theme, $path)
{
    return array(
        'user_register' => array(
            'render element' => 'form',
            'template'       => 'user-register',
        ),
        'user_login'    => array(
            'render element' => 'form',
            'template'       => 'user-login',
        ),
    );
}

function string_wrap($string, $width = 10, $break = "&shy", $cut = true)
{
    $charset = mb_internal_encoding();

    $words  = mb_split(' ', $string);
    $result = array();

    foreach ($words as &$word) {
        $word_parts = array();

        while ($cut && mb_strlen($word) > $width) {
            $word_parts[] = mb_substr($word, 0, $width, $charset);
            $word         = mb_substr($word, $width, 2048, $charset);
        }

        $word_parts[] = $word;
        $result[]     = implode($break, $word_parts);
    }

    return implode(' ', $result);
}